package com.example.xdemo;

import ohos.agp.components.*;
import ohos.app.Context;

import java.util.HashMap;
import java.util.List;

import static ohos.agp.utils.LayoutAlignment.TOP;

public abstract class RecyclerAdapter<T> extends RecycleItemProvider {

    private Context context;
    private List<T> data;
    private int mLayoutId;
    private int numColumns = 1;
    private OnItemClickListener onItemClickListener;

    /**
     * @param context   上下文
     * @param data      数据源
     * @param mLayoutId 条目的资源文件id
     * @return
     */
    public RecyclerAdapter(Context context, List<T> data, int mLayoutId) {
        this.context = context;
        this.data = data;
        this.mLayoutId = mLayoutId;
    }

    public List<T> getData() {
        return data;
    }

    @Override
    public int getCount() {
        return data.size() % numColumns == 0 ? data.size() / numColumns : data.size() / numColumns + 1;
    }

    @Override
    public T getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer parent) {
        ViewHolder viewHolder;
        convertComponent = new DirectionalLayout(context);
        ((DirectionalLayout) convertComponent).setOrientation(Component.HORIZONTAL);
        ComponentContainer.LayoutConfig layoutConfig = convertComponent.getLayoutConfig();
        layoutConfig.width = DependentLayout.LayoutConfig.MATCH_PARENT;
        convertComponent.setLayoutConfig(layoutConfig);

        for (int i = 0; i < numColumns; i++) {
            if (position * numColumns + i < data.size()) {
                DirectionalLayout dlItemParent = new DirectionalLayout(context);
                dlItemParent.setLayoutConfig(new DirectionalLayout.LayoutConfig(0, DirectionalLayout.LayoutConfig.MATCH_CONTENT, TOP, 1));
                Component childConvertComponent = LayoutScatter.getInstance(context).parse(mLayoutId, null, false);
//                int finalI = i;
//                childConvertComponent.setClickedListener(new Component.ClickedListener() {
//                    @Override
//                    public void onClick(Component component) {
//                        onItemClickListener.onItemClick(component, position * numColumns + finalI);
//                    }
//                });
                dlItemParent.addComponent(childConvertComponent);
                ((ComponentContainer) convertComponent).addComponent(dlItemParent);
                viewHolder = new ViewHolder(childConvertComponent);
                bind(viewHolder, getItem(position * numColumns + i), position * numColumns + i);
            } else {
                Component childConvertComponent = new Component(context);
                DirectionalLayout.LayoutConfig layoutConfig1 = new DirectionalLayout.LayoutConfig(0, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
                layoutConfig1.weight = 1;
                childConvertComponent.setLayoutConfig(layoutConfig1);
                ((ComponentContainer) convertComponent).addComponent(childConvertComponent);
            }
        }
        return convertComponent;
    }

    public void setNumColumns(int numColumns) {
        this.numColumns = numColumns;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    protected abstract void bind(ViewHolder holder, T s, int position);

    public static class ViewHolder  {
        HashMap<Integer, Component> mViews = new HashMap<>();
        public Component itemView;
        public ViewHolder(Component component) {
            this.itemView = component;
        }

        public <E extends Component> E getView(int viewId) {
            E view = (E) mViews.get(viewId);
            if (view == null) {
                view = (E) itemView.findComponentById(viewId);
                mViews.put(viewId, view);
            }
            return view;
        }
    }

    public static abstract class OnItemClickListener {
        public abstract void onItemClick(Component component, int position);
    }

}
